package services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;

import models.DrCourt;
import models.DrLiveTemp;
import models.DrMicrophone;
import models.DrMicrophoneExample;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrMicrophoneMapper;
import models.mapper.SystemLogMapper;

/**
 * 麦克风服务类
 * @author wangshaoming
 *
 */
public class DrMicroService {
	/**
	 * 根据法庭获取麦克风列表
	 * @return
	 */
	public static List<DrMicrophone> getMicroByCourt(String courtid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			DrMicrophoneExample example=new DrMicrophoneExample();
			example.setOrderByClause("microid");
			DrMicrophoneExample.Criteria c=example.createCriteria();
			c.andBlongcourtEqualTo(courtid);
			c.andStatusEqualTo(new BigDecimal(1));
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}

	public static boolean addDrMicro(DrMicrophone microphone, SystemOperator loginUser,
			String remoteAddress) {
				SqlSession ss=SessionFactory.getSqlSession();
				try{
					DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
					mapper.insert(microphone);
					
					SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),remoteAddress,"新增麦克风数据","新增麦克风数据",new Date());
					SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
					logMapper.insert(log);
				}catch(Exception e){
					e.printStackTrace();
					ss.rollback();
					return false;
				}finally{
					ss.close();
				}
				return true;
		
	}

	public static boolean updateDrMicro(DrMicrophone microPhone,
			SystemOperator loginUser, String remoteAddress) {
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			mapper.updateByPrimaryKeySelective(microPhone);
			
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),remoteAddress,"更新麦克风数据","更新麦克风数据",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
		
	}

	public static void deleteByCourtid(String courtid) {
		
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			DrMicrophoneExample example=new DrMicrophoneExample();
			DrMicrophoneExample.Criteria c=example.createCriteria();
			c.andBlongcourtEqualTo(courtid);
			mapper.deleteByExample(example);
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取麦克风对象
	 * @param role
	 * @return
	 */
	public static DrMicrophone getById(BigDecimal role) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			return mapper.selectByPrimaryKey(role.longValue());
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据主键获取id集合
	 * @param ids
	 * @param courtid 
	 * @return
	 */
	public static List<DrMicrophone> getByIds(List<Long> ids,Boolean isBlong, String courtid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			DrMicrophoneExample example=new DrMicrophoneExample();
			DrMicrophoneExample.Criteria c=example.createCriteria();
			c.andMicroidIn(ids);
			if(isBlong){
				c.andBlongcourtNotEqualTo(courtid);
			}
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据法庭获取麦克风数量
	 * @param courtid 法庭编号
	 * @return
	 */
	public static int countByCourt(String courtid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrMicrophoneMapper mapper=ss.getMapper(DrMicrophoneMapper.class);
			DrMicrophoneExample example=new DrMicrophoneExample();
			DrMicrophoneExample.Criteria c=example.createCriteria();
			c.andBlongcourtEqualTo(courtid);
			return mapper.countByExample(example);
		}finally{
			ss.close();
		}
	}

}
